package com.ark.service.service.impl;

import com.ark.common.domain.entity.game.GameInfo;
import com.ark.common.domain.entity.sys.SysUser;
import com.ark.common.exception.ServiceException;
import com.ark.common.response.Result;
import com.ark.common.utils.EncryptionUtils;
import com.ark.common.utils.SecurityUtils;
import com.ark.service.mapper.GameInfoMapper;
import com.ark.service.service.GameInfoService;
import com.ark.service.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class GameInfoServiceImpl extends ServiceImpl<GameInfoMapper, GameInfo> implements GameInfoService {

    @Resource
    private GameInfoMapper gameInfoMapper;

    @Resource
    private SysUserService userService;


    @Override
    @Transactional
    public boolean insertGameInfo(GameInfo gameInfo,String email) {
        if (userService.checkUserNameUnique(gameInfo.getGameAccount())){
            throw new ServiceException(Result.error("账号已被添加"));
        }
        //填充用户数据
        SysUser user = new SysUser();
        user.setNickname(gameInfo.getGameAccount());
        user.setUsername(gameInfo.getGameAccount());
        //加密平台密码为不可逆
        user.setPassword(SecurityUtils.encrypt(gameInfo.getGamePassword()));
        user.setEmail(email);
        //插入用户
        userService.insertUser(user);
        //填充游戏数据
        gameInfo.setNickname(gameInfo.getGameAccount());
        gameInfo.setGamePassword(EncryptionUtils.encrypt(gameInfo.getGamePassword()));
        gameInfo.setAgentId(SecurityUtils.getUserId());
        gameInfo.setUserId(user.getId());
        return save(gameInfo);
    }

    @Override
    public IPage<GameInfo> selectList( Page<GameInfo> page, GameInfo gameInfo) {
        LambdaQueryWrapper<GameInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(gameInfo.getGameAccount()), GameInfo::getGameAccount, gameInfo.getGameAccount())
                .like(Objects.nonNull(gameInfo.getNickname()), GameInfo::getNickname, gameInfo.getNickname());
        return gameInfoMapper.selectPage(page, wrapper);
    }

    @Override
    public Map<String,List<GameInfo>> categoryList() {
        Map<String,List<GameInfo>> map = new HashMap<>();
        //按插队优先级排序
        map.put("pending", lambdaQuery().eq(GameInfo::getStatus,0).orderByDesc(GameInfo::getPriorityTime).list());
        map.put("inProgress", lambdaQuery().eq(GameInfo::getStatus,1).orderByDesc(GameInfo::getPriorityTime).list());
        map.put("completed", lambdaQuery().eq(GameInfo::getStatus,2).orderByDesc(GameInfo::getPriorityTime).list());
        map.put("exception", lambdaQuery().eq(GameInfo::getStatus,3).orderByDesc(GameInfo::getPriorityTime).list());
        return map;
    }

    @Override
    public GameInfo getGameInfo() {
        GameInfo gameInfo=lambdaQuery().one();
        if (Objects.isNull(gameInfo)){
            throw new ServiceException(Result.error("获取游戏账号信息异常"));
        }
        try {
            gameInfo.setGamePassword(EncryptionUtils.decrypt(gameInfo.getGamePassword()));
        }catch (Exception e) {
            throw new ServiceException(Result.error("解密游戏密码失败"));
        }
        return gameInfo;
    }
}
